package com.intellagric.service.agritech.impl;

import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.common.utils.UUIDUtils;
import com.intellagric.mapper.AgtTagMapper;
import com.intellagric.pojo.AgtTag;
import com.intellagric.pojo.AgtTagExample;
import com.intellagric.service.agritech.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: cwq
 * @Date: 2019/2/21 13:46
 * @Description:
 */
@Service
public class TagServiceImpl implements TagService {

    @Autowired
    private AgtTagMapper agtTagMapper;

    @Override
    public ResponseMessage addTag(AgtTag tag) {
        tag.setId(UUIDUtils.getID());
        tag.setCreatedTime(new Date());
        return agtTagMapper.insertSelective(tag) == 1 ? ResponseMessage.success().add("add", tag) : ResponseMessage.fail();
    }

    @Override
    public ResponseMessage deleteTag(String id) {
        return agtTagMapper.deleteByPrimaryKey(id) == 1 ? ResponseMessage.success() : ResponseMessage.fail();
    }

    @Override
    public ResponseMessage editTag(AgtTag tag) {
        return agtTagMapper.updateByPrimaryKeySelective(tag) == 1 ? ResponseMessage.success().add("update", tag) : ResponseMessage.fail();
    }

    @Override
    public AgtTag getTagById(String id) {
        return agtTagMapper.selectByPrimaryKey(id);
    }

    @Override
    public LayuiDataGridResult getTagList() {
        AgtTagExample agtTagExample = new AgtTagExample();
        agtTagExample.setOrderByClause("created_time ASC");
        agtTagExample.createCriteria().andIdIsNotNull();
        List<AgtTag> agtTagList = agtTagMapper.selectByExample(agtTagExample);
        return LayuiDataGridResult.success().add(agtTagList, agtTagList.size());
    }

    @Override
    public LayuiDataGridResult getTagByPid(String pId) {
        AgtTagExample agtTagExample = new AgtTagExample();
        agtTagExample.setOrderByClause("created_time ASC");
        agtTagExample.createCriteria().andPidEqualTo(pId);
        List<AgtTag> agtTagList = agtTagMapper.selectByExample(agtTagExample);
        return LayuiDataGridResult.success().add(agtTagList, agtTagList.size());
    }

    @Override
    public LayuiDataGridResult getTagTree() {
        //获取所有分类
        List<AgtTag> agtTagList = this.getTagList().getData();

        //获取顶级分类
        List<AgtTag> rootAgtTagList = this.getTagByPid("0").getData();

        for (AgtTag agtTag : rootAgtTagList) {
            //每个分类项目都设置其子分类
            agtTag.setChildren(this.getChild(agtTag.getId(),agtTagList));
        }

        return LayuiDataGridResult.success().add(rootAgtTagList, rootAgtTagList.size());
    }

    private List<AgtTag> getChild(String id, List<AgtTag> agtTagList) {
        //返回的子菜单
        List<AgtTag> childList = new ArrayList<>();

        // 遍历所有节点，将父菜单id与传过来的id比较
        for (AgtTag agtTag : agtTagList) {
            if (agtTag.getPid() != null) {
                //如果列表中的父id等于当前的分类的id，那就加入到该分类子列表中
                if (agtTag.getPid().equals(id)) {
                    childList.add(agtTag);
                }
            }
        }

        // 把子菜单的子菜单childList再循环一遍
        for (AgtTag agtTag : childList) {
            agtTag.setChildren(this.getChild(agtTag.getId(),agtTagList));
        }

        //退出递归的条件
        if (childList.size() == 0) {
            return null;
        }

        return childList;
    }
}
